<!--
 * @Description: 文章项组件
 * @Author: snows_l snows_l@163.com
 * @Date: 2025-01-27 00:00:00
 * @LastEditors: snows_l snows_l@163.com
 * @LastEditTime: 2025-11-03 17:35:29
 * @FilePath: \BLOG\src\components\ArticleItem\index.vue
-->
<template>
  <div class="item-wrap" :class="{ 'img-left': !isMobi && index % 2 === 0, 'img-right': !isMobi && index % 2 !== 0 }">
    <!-- 移动端图片布局 -->
    <div v-if="isMobi" class="cover-img-wrap pointer" @click="$emit('article-click', item)">
      <LImg class="cover-img" :size="isMobi ? '120px' : '160px'" :src="item.cover" :isUnPreview="true" />
    </div>
    <!-- PC端左侧图片布局 -->
    <div v-if="!isMobi && index % 2 === 0" class="cover-img-wrap pointer" @click="$emit('article-click', item)">
      <LImg class="cover-img" :size="isMobi ? '120px' : '160px'" :src="item.cover" :fit="isMobi ? 'contain' : ''" :isUnPreview="true" />
    </div>

    <div class="item-content">
      <div class="create-time">
        <div class="time-wrap">
          <IconCalendar class="iconfont"></IconCalendar>
          <span>发布于：</span>
          <span class="time">{{ item.createTime }}</span>
        </div>
      </div>
      <div class="article-title-warp">
        <div class="article-title pointer" @click="$emit('article-click', item)">
          <div class="title">
            {{ item.title }}
          </div>
        </div>
      </div>
      <div class="auth-info-wrap">
        <div>
          <img :src="avatar" alt="" />
          <span>冬七</span>
        </div>
        <div>
          <IconHot class="iconfont"></IconHot>
          <span>{{ item.readCount || 0 }}</span>
        </div>
        <div>
          <IconLike :size="18" class="iconfont" />
          <span>{{ item.shareCount || 0 }}</span>
        </div>
        <div>
          <IconComment :size="19" class="iconfont"></IconComment>
          <span>{{ item.commentCount || 0 }}</span>
        </div>
        <div class="pointer" v-if="item.isPreview === 1" @click="$emit('view-click', item)">
          <IconPreview class="iconfont" :size="18"></IconPreview>
          <span class="" style="font-size: 12px">预览</span>
        </div>
      </div>
      <div class="article-des text">
        {{ item.subTitle }}
      </div>
      <div class="label-wrap">
        <div class="type pointer hover-shadow" @click="$emit('type-click', item.type)">
          <span class="icon-wrap">
            <IconType class="iconfont"></IconType>
          </span>
          <span class="lable" :style="{ 'background-color': 'var(--theme-light-color-2)' }">
            {{ getArticleTypeLabel(item.type) }}
          </span>
        </div>
        <div class="label-item pointer hover-shadow" @click="$emit('category-click', label)" v-for="(label, labelIndex) in item.labels" :key="labelIndex">
          <span class="icon-wrap">
            <IconLabel class="iconfont" :size="'14px'"></IconLabel>
          </span>
          <span class="lable" :style="{ 'background-color': colorMap[labelIndex] }">{{ label }}</span>
        </div>
      </div>
    </div>

    <!-- PC端右侧图片布局 -->
    <div v-if="!isMobi && index % 2 !== 0" class="cover-img-wrap pointer" @click="$emit('article-click', item)">
      <LImg class="cover-img" :size="isMobi ? '120px' : '160px'" :src="item.cover" :isUnPreview="true" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';

interface Props {
  item: any;
  index: number;
  isMobi: boolean;
  articleTypeList: any[];
  colorMap: Record<number, string>;
  avatar: string;
}

const props = defineProps<Props>();

defineEmits<{
  'article-click': [item: any];
  'view-click': [item: any];
  'type-click': [type: number];
  'category-click': [label: string];
}>();

// 使用计算属性优化类型查找
const getArticleTypeLabel = computed(() => {
  return (type: number) => {
    return props.articleTypeList.find(v => v.value == type)?.label || '未知类型';
  };
});
</script>

<style lang="scss" scoped>
.item-wrap {
  display: flex;
  width: 100%;
  overflow: hidden;
  .cover-img-wrap {
    width: 450px;
    height: 280px;
    overflow: hidden;
    .cover-img {
      width: 100%;
      height: 100%;
      transition: transform 0.8s ease-in-out;
    }
  }
  &:hover {
    .cover-img {
      transform: scale(1.1);
    }
  }
}

.item-content {
  flex: 1;
  height: 280px;
  padding: 33px 30px;
  background-color: var(--bg-content-color);
  position: relative;

  .create-time {
    display: inline-block;
    align-items: center;
    height: 24px;
    font-size: 12px;
    padding: 2px 0px;
    border-radius: 5px;
    color: var(--text-color-2);
    .time-wrap {
      height: 100%;
      display: flex;
      align-items: center;
      .iconfont {
        font-size: 12px;
        margin-right: 10px;
      }
    }
  }

  .article-title {
    display: inline-block;
    .title {
      margin-top: 10px;
      height: 30px;
      font-size: 20px;
      line-height: 24px;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 1;
      overflow: hidden;
      text-overflow: ellipsis;
      text-align: left;
      color: var(--text-color);
      position: relative;
      &:hover {
        color: var(--theme-color);
        &::after {
          width: 101%;
        }
      }
      &:after,
      &:after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 0;
        height: 3px;
        width: 0%;
        border-radius: 5px;
        background-color: var(--theme-color);
        transition: width 0.3s ease-in;
      }
    }
  }

  .auth-info-wrap {
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-between;

    div {
      display: flex;
      align-items: center;
      .iconfont {
        margin-right: 10px;
        font-size: 18px;
        color: var(--theme-color);
      }
      .icon-yanjing {
        font-size: 24px;
      }
      span {
        font-size: 12px;
        color: var(--text-color);
      }
    }

    img {
      width: 30px;
      height: 30px;
      border-radius: 50%;
      margin-right: 10px;
    }
  }

  .article-des {
    height: 64px;
    line-height: 21px;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--text-color-2);
  }
}

.label-wrap {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  flex-wrap: wrap;
  margin-top: 10px;

  .type {
    font-size: 12px;
    border-radius: 5px;
    margin-right: 15px;
    height: 20px;
    display: flex;
    align-items: center;
    overflow: hidden;
    margin-bottom: 2px;
    .icon-wrap {
      padding: 1px 6px 1px 8px;
      border-right: 1px solid var(--border-color);
    }
  }

  .label-item {
    margin-right: 10px;
    font-size: 12px;
    display: flex;
    align-items: center;
    border-radius: var(--border-radius-1);
    overflow: hidden;
    height: 20px;
    margin-bottom: 2px;
  }

  .icon-wrap,
  .lable {
    height: 100%;
    display: inline-block;
  }

  .icon-wrap {
    padding: 3px 6px 3px 8px;
    background-color: #b3b0b0;
    border-right: 1px solid var(--border-color);
  }

  .lable {
    color: #fff;
    padding: 4px 10px 4px 6px !important;
    font-size: 12px;
    text-wrap: nowrap;
  }
}

// 移动端样式
@media (max-width: 768px) {
  .item-wrap {
    display: flex;
    flex-direction: column;
    height: auto !important;

    .cover-img-wrap {
      width: 100% !important;
      height: 180px !important;
    }

    .item-content {
      height: 180px !important;
      padding: 20px 20px !important;

      .article-title {
        margin-top: 5px !important;
        height: 30px !important;
        line-height: 30px !important;
        font-size: 16px !important;
      }
    }

    .auth-info-wrap {
      height: 30px !important;
      img {
        width: 20px !important;
        height: 20px !important;
      }
    }

    .article-des {
      height: 30px !important;
      line-height: 15px !important;
      font-size: 12px !important;
      -webkit-line-clamp: 2 !important;
    }
  }
}
</style>
